home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / dev / moni / SystemViewer.lha / Source / SysMemViewer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-30  |  12.8 KB  |  535 lines

  1. /****h *SysMemory/SysMemViewer.c ************************************
  2. **
  3. ** NAME
  4. **    SysMemViewer.c
  5. **
  6. ** DESCRIPTION
  7. **    These functions form a GUI for the SysMemory program so that
  8. **    the user can view the memory that was selected from SysMemory.
  9. **
  10. ** FUNCTIONAL INTERFACE:
  11. **
  12. **    PUBLIC int ViewMemory( int type, ULONG startaddress );
  13. **
  14. ** GUI Designed by : Jim Steichen
  15. *********************************************************************
  16. */
  17.  
  18. #include <string.h>
  19.  
  20. #include <exec/types.h>
  21.  
  22. #include <AmigaDOSErrs.h>
  23.  
  24. #include <intuition/intuition.h>
  25. #include <intuition/classes.h>
  26. #include <intuition/classusr.h>
  27. #include <intuition/gadgetclass.h>
  28.  
  29. #include <libraries/gadtools.h>
  30.  
  31. #include <graphics/displayinfo.h>
  32. #include <graphics/gfxbase.h>
  33.  
  34. #include <clib/exec_protos.h>
  35. #include <clib/intuition_protos.h>
  36. #include <clib/gadtools_protos.h>
  37. #include <clib/graphics_protos.h>
  38. #include <clib/utility_protos.h>
  39. #include <clib/diskfont_protos.h>
  40.  
  41. #include "CPGM:GlobalObjects/CommonFuncs.h"
  42.  
  43. #define StrBfPtr( g ) (((struct StringInfo *)g->SpecialInfo)->Buffer)
  44.  
  45. #define MemLV      0
  46. #define StartAddr  1
  47. #define NextPage   2
  48. #define PrevPage   3
  49. #define DoneBt     4
  50. #define Update     5
  51.  
  52. #define MV_CNT     6
  53.  
  54. #define START_ADDR_STRING StrBfPtr( MVGadgets[ StartAddr ] )
  55.  
  56. IMPORT struct Screen *Scr;
  57. IMPORT UBYTE         *PubScreenName;
  58. IMPORT APTR           VisualInfo;
  59.  
  60. IMPORT struct TextAttr *Font, Attr;
  61. IMPORT struct TextFont *MVFont = NULL;
  62.  
  63. IMPORT struct CompFont  CFont;
  64.  
  65. PRIVATE struct Window       *MVWnd   = NULL;
  66. PRIVATE struct Gadget       *MVGList = NULL;
  67. PRIVATE struct IntuiMessage  MVMsg;
  68. PRIVATE struct Gadget       *MVGadgets[ MV_CNT ];
  69.  
  70. PRIVATE UWORD  MVLeft   = 38;
  71. PRIVATE UWORD  MVTop    = 16;
  72. PRIVATE UWORD  MVWidth  = 540;
  73. PRIVATE UWORD  MVHeight = 296;
  74. PRIVATE UBYTE *MVWdt    = (UBYTE *) "System Memory Viewer ";
  75.  
  76. // -------------------------------------------------------------------
  77.  
  78. #define MAXNODE    20
  79. #define NODELENGTH 64
  80.  
  81. PRIVATE struct MinList MemLVList;
  82.  
  83. PRIVATE struct Node MemLVNode;
  84. PRIVATE struct Node MemLVNodes[ MAXNODE ] = { NULL, };
  85.  
  86. PRIVATE UBYTE       NodeStrs[ MAXNODE * NODELENGTH ] = "";
  87.  
  88. // -------------------------------------------------------------------
  89.  
  90. PRIVATE UWORD MVGTypes[] = {
  91.  
  92.    LISTVIEW_KIND, STRING_KIND, BUTTON_KIND,
  93.    BUTTON_KIND,   BUTTON_KIND,  BUTTON_KIND
  94. };
  95.  
  96. PRIVATE int MemLVClicked(     void );
  97. PRIVATE int StartAddrClicked( void );
  98. PRIVATE int NextPageClicked(  void );
  99. PRIVATE int PrevPageClicked(  void );
  100. PRIVATE int DoneBtClicked(    void );
  101. PRIVATE int UpdateClicked(    void );
  102.  
  103. PRIVATE struct NewGadget MVNGad[] = {
  104.  
  105.      4,  27, 531, 240, (UBYTE *) "Memory Contents:", NULL, MemLV,
  106.    PLACETEXT_ABOVE, NULL, (APTR) MemLVClicked,
  107.   
  108.    106, 269,  81,  18, (UBYTE *) "Start Addr:",      NULL, StartAddr,
  109.    PLACETEXT_LEFT, NULL, (APTR) StartAddrClicked,
  110.  
  111.    198, 269,  87,  18, (UBYTE *) "_Next Page",       NULL, NextPage,
  112.    PLACETEXT_IN, NULL, (APTR) NextPageClicked,
  113.  
  114.    294, 269,  87,  18, (UBYTE *) "_Prev Page",       NULL, PrevPage,
  115.    PLACETEXT_IN, NULL, (APTR) PrevPageClicked,
  116.  
  117.    467, 269,  67,  18, (UBYTE *) "_Done!",           NULL, DoneBt,
  118.    PLACETEXT_IN, NULL, (APTR) DoneBtClicked,
  119.  
  120.    385, 236,  75,  18, (UBYTE *) "_Update",          NULL, Update, 
  121.    PLACETEXT_IN, NULL, (APTR) UpdateClicked
  122. };
  123.  
  124. PRIVATE ULONG MVGTags[] = {
  125.  
  126.    GTLV_Labels, (ULONG) &MemLV0List, GTLV_ReadOnly, TRUE, 
  127.    LAYOUTA_Spacing, 2, TAG_DONE,
  128.  
  129.    GA_TabCycle, FALSE, GTST_String, (STRPTR) "00000000", 
  130.    GTST_MaxChars, 10, 
  131.    STRINGA_Justification, GACT_STRINGCENTER, TAG_DONE,
  132.  
  133.    GT_Underscore, '_', TAG_DONE,
  134.    GT_Underscore, '_', TAG_DONE,
  135.    GT_Underscore, '_', TAG_DONE,
  136.    GT_Underscore, '_', TAG_DONE
  137. };
  138.  
  139.  
  140. PRIVATE int MemLVClicked( void )
  141. {
  142.    return( (int) TRUE );
  143. }
  144.  
  145. PRIVATE UBYTE t[80], *title = &t[0];
  146.  
  147. PRIVATE void UpdateTitle( ULONG address )
  148. {
  149.    sprintf( title, "%s (at %08LX):", MVWdt, address );
  150.    SetWindowTitles( MVWnd, title, (UBYTE *) -1 );
  151.    return;
  152. }
  153.  
  154. PRIVATE ULONG PrevAddress    = 0L;
  155. PRIVATE ULONG CurrentAddress = 0L;
  156.  
  157. PRIVATE int StartAddrClicked( void )
  158. {
  159.    PrevAddress = CurrentAddress;
  160.  
  161.    // Convert the string gadget contents to an address:   
  162.    (void) stch_l( START_ADDR_STRING, &CurrentAddress );
  163.  
  164.    if (CurrentAddress < 0)
  165.       {
  166.       // Tell the user what a bonehead he is:
  167.       (void) Handle_Problem( "", "", NULL );
  168.       
  169.       CurrentAddress = PrevAddress;
  170.       }
  171.          
  172.    UpdateTitle( CurrentAddress );
  173.    return( (int) TRUE );
  174. }
  175.  
  176. PRIVATE int NextPageClicked( void )
  177. {
  178.    PrevAddress = CurrentAddress;
  179.  
  180.    // Kill old nodes & re-calculate the display:
  181.  
  182.    CurrentAddress += (16 * MAXNODE);
  183.  
  184.    UpdateTitle( CurrentAddress );
  185.    return( (int) TRUE );
  186. }
  187.  
  188. PRIVATE int PrevPageClicked( void )
  189. {
  190.    /* routine when gadget "_Prev Page" is clicked. */
  191.    CurrentAddress = PrevAddress;
  192.  
  193.    UpdateTitle( CurrentAddress );
  194.    return( (int) TRUE );
  195. }
  196.  
  197. PRIVATE void CloseMVWindow( void )
  198. {
  199.    if (MVWnd != NULL) 
  200.       {
  201.       CloseWindow( MVWnd );
  202.       MVWnd = NULL;
  203.       }
  204.  
  205.    if (MVGList != NULL) 
  206.       {
  207.       FreeGadgets( MVGList );
  208.       MVGList = NULL;
  209.       }
  210.  
  211.    if (MVFont != NULL) 
  212.       {
  213.       CloseFont( MVFont );
  214.       MVFont = NULL;
  215.       }
  216.  
  217.    return;
  218. }
  219.  
  220. PRIVATE int MVCloseWindow( void )
  221. {
  222.    CloseMVWindow();
  223.    return( (int) FALSE );
  224. }
  225.  
  226. PRIVATE int DoneBtClicked( void )
  227. {
  228.    return( MVCloseWindow() );
  229. }
  230.  
  231. PRIVATE int UpdateClicked( void )
  232. {
  233.    int   i;
  234.    ULONG temp = (ULONG) CurrentAddress;
  235.    
  236.    HideListFromView( MVGadgets[ MemLV ], MVWnd );
  237.  
  238.    for (i = 1; i <= MAXNODE; i++)
  239.        NodeStrs[ i * NODELENGTH ] = '\0'; // Kill old ListView strings.
  240.  
  241.    // Re-construct the memory strings, starting at CurrentAddress:
  242.    
  243.    for (i = 0; i < MAXNODE; i++)
  244.       {
  245.       (void) MakeHexASCIIStr( &NodeStrs[i * NODELENGTH ], 
  246.                               (char *) temp, 16 );
  247.       temp += 16;
  248.       }
  249.  
  250.    ModifyListView( MVGadgets[ MemLV ], MVWnd, &MemLVList, NULL ); 
  251.    return( (int) TRUE );
  252. }
  253.  
  254. PRIVATE void MVRender( char *description )
  255. {
  256.    struct IntuiText it;
  257.    char             d[80], *txt = &d[0];
  258.  
  259.    // "You Selected Expansion Memory (07000000-08000000):"
  260.    // "You Selected Chip Memory (00001000-001FFFFF):"
  261.  
  262.    ComputeFont( Scr, Font, &CFont, MVWidth, MVHeight );
  263.  
  264.    strcpy( txt, description ); 
  265.  
  266.    it.NextText  = NULL;
  267.    it.FrontPen  = 2;
  268.    it.BackPen   = 0;
  269.    it.DrawMode  = JAM1;
  270.    it.IText     = (UBYTE *) txt;       
  271.    it.ITextFont = Font;
  272.    it.LeftEdge  = 277;
  273.    it.TopEdge   = 6;
  274.    
  275.    it.LeftEdge  = CFont.OffX + ComputeX( CFont.FontX, it.LeftEdge ) 
  276.                   - (IntuiTextLength( &it ) >> 1);
  277.       
  278.    it.TopEdge   = CFont.OffY + ComputeY( CFont.FontY, it.TopEdge ) 
  279.                   - (Font->ta_YSize >> 1);
  280.  
  281.    PrintIText( MVWnd->RPort, &it, 0, 0 );
  282.  
  283.    return;
  284. }
  285.  
  286. PRIVATE int OpenMVWindow( char *description )
  287. {
  288.    struct NewGadget  ng;
  289.    struct Gadget    *g;
  290.    UWORD             lc, tc;
  291.    UWORD             wleft = MVLeft, wtop = MVTop, ww, wh;
  292.  
  293.    ComputeFont( Scr, Font, &CFont, MVWidth, MVHeight );
  294.  
  295.    ww = ComputeX( CFont.FontX, MVWidth );
  296.    wh = ComputeY( CFont.FontY, MVHeight );
  297.  
  298.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width) 
  299.       wleft = Scr->Width - ww;
  300.    
  301.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height) 
  302.       wtop = Scr->Height - wh;
  303.  
  304.    if ( !(MVFont = OpenDiskFont( Font )))
  305.       return( -5 );
  306.  
  307.    if ( !(g = CreateContext( &MVGList )))
  308.       return( -1 );
  309.  
  310.    for (lc = 0, tc = 0; lc < MV_CNT; lc++) 
  311.       {
  312.       CopyMem( (char *) &MVNGad[ lc ], (char *) &ng, 
  313.                (long) sizeof( struct NewGadget )
  314.              );
  315.  
  316.       ng.ng_VisualInfo = VisualInfo;
  317.       ng.ng_TextAttr   = Font;
  318.       ng.ng_LeftEdge   = CFont.OffX + ComputeX( CFont.FontX,
  319.                                                 ng.ng_LeftEdge
  320.                                               );
  321.  
  322.       ng.ng_TopEdge    = CFont.OffY + ComputeY( CFont.FontY,
  323.                                                 ng.ng_TopEdge
  324.                                               );
  325.  
  326.       ng.ng_Width      = ComputeX( CFont.FontX, ng.ng_Width );
  327.       ng.ng_Height     = ComputeY( CFont.FontY, ng.ng_Height);
  328.  
  329.       MVGadgets[ lc ] = g = CreateGadgetA( (ULONG) MVGTypes[ lc ], 
  330.                               g, 
  331.                               &ng, 
  332.                               (struct TagItem *) &MVGTags[ tc ] );
  333.  
  334.       while (MVGTags[ tc ]) 
  335.          tc += 2;
  336.       
  337.       tc++;
  338.  
  339.       if (NOT g)
  340.          return( -2 );
  341.       }
  342.  
  343.    if ( !(MVWnd = OpenWindowTags( NULL,
  344.  
  345.             WA_Left,        wleft,
  346.             WA_Top,         wtop,
  347.             WA_Width,       ww + CFont.OffX + Scr->WBorRight,
  348.             WA_Height,      wh + CFont.OffY + Scr->WBorBottom,
  349.             WA_IDCMP,       LISTVIEWIDCMP | INTEGERIDCMP | BUTTONIDCMP
  350.               | IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_REFRESHWINDOW,
  351.  
  352.             WA_Flags,       WFLG_DRAGBAR | WFLG_DEPTHGADGET
  353.               | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_ACTIVATE
  354.               | WFLG_RMBTRAP,
  355.  
  356.             WA_Gadgets,     MVGList,
  357.             WA_Title,       MVWdt,
  358.             WA_ScreenTitle, "System Memory Viewer ©1999 by J.T. Steichen",
  359.             TAG_DONE ))
  360.       )
  361.       return( -4 );
  362.  
  363.    GT_RefreshWindow( MVWnd, NULL );
  364.    MVRender( description );
  365.    return( 0 );
  366. }
  367.  
  368. PRIVATE int MVVanillaKey( int whichkey )
  369. {
  370.    int rval = TRUE;
  371.    
  372.    switch (whichkey)
  373.       {
  374.       case 'd':
  375.       case 'D':
  376.       case 'q':
  377.       case 'Q':
  378.          rval = DoneBtClicked();
  379.          break;
  380.          
  381.       case 'p':
  382.       case 'P':
  383.          rval = PrevPageClicked();
  384.          break;
  385.          
  386.       case 'n':
  387.       case 'N':
  388.  
  389.          rval = NextPageClicked();
  390.          break;
  391.  
  392.       case 'u':
  393.       case 'U':
  394.          rval = UpdateClicked();
  395.          break;
  396.                
  397.       default:
  398.          break;
  399.       }
  400.       
  401.    return( rval );
  402. }
  403.  
  404. PRIVATE int HandleMVIDCMP( void )
  405. {
  406.    struct IntuiMessage  *m;
  407.    int                 (*func)( void );
  408.    BOOL                  running = TRUE;
  409.  
  410.    while (running == TRUE)
  411.       {
  412.       if ((m = GT_GetIMsg( MVWnd->UserPort )) == NULL) 
  413.          {
  414.          (void) Wait( 1L << MVWnd->UserPort->mp_SigBit );
  415.          continue;
  416.          }
  417.  
  418.       CopyMem( (char *) m, (char *) &MVMsg, 
  419.                (long) sizeof( struct IntuiMessage )
  420.              );
  421.  
  422.       GT_ReplyIMsg( m );
  423.  
  424.       switch (MVMsg.Class) 
  425.          {
  426.          case IDCMP_REFRESHWINDOW:
  427.             GT_BeginRefresh( MVWnd );
  428.             MVRender();
  429.             GT_EndRefresh( MVWnd, TRUE );
  430.             break;
  431.  
  432.          case IDCMP_CLOSEWINDOW:
  433.             running = MVCloseWindow();
  434.             break;
  435.  
  436.          case IDCMP_VANILLAKEY:
  437.             running = MVVanillaKey( (int) MVMsg.Code );
  438.             break;
  439.  
  440.          case IDCMP_GADGETUP:
  441.          // case IDCMP_GADGETDOWN:
  442.             
  443.             func = (void *) ((struct Gadget *)MVMsg.IAddress)->UserData;
  444.             
  445.             if (func != NULL)
  446.                running = func();
  447.             
  448.             break;
  449.          }
  450.       }
  451.  
  452.    return( running );
  453. }
  454.  
  455. /****i *InitMemoryNodes() ---------------------------------------------
  456. **
  457. ** NAME
  458. **    InitMemoryNodes()
  459. **
  460. ** NOTES
  461. **    This is what a typical list item might look like:
  462. **
  463. **    "00000000: aaaaffff 22334455 22334455 66554433 ................"
  464. ** --------------------------------------------------------------------
  465. */
  466.  
  467. PRIVATE int InitMemoryNodes( ULONG startaddress )
  468. {
  469.    int i = 0;
  470.    
  471.    MVLVNode.ln_Succ = (struct Node *) MVLVList.mlh_Tail;
  472.    MVLVNode.ln_Pred = (struct Node *) MVLVList.mlh_Head;
  473.    MVLVNode.ln_Type = 0;
  474.  
  475.    MVLVNodes[0] = MVLVNode;
  476.  
  477.    SetNotifyWindow( MVWnd );
  478.  
  479.    for (i = 0; i < MAXNODE; i++)
  480.       {
  481.       MVLVNodes[i].ln_Name = &NodeStrs[ i * NODELENGTH ];
  482.       MVLVNodes[i].ln_Pri  = MAXNODE - i;
  483.       }
  484.  
  485.    NewList( (struct List *) &MVLVList );      
  486.  
  487.    for (i = 0; i < MAXNODE; i++)
  488.       Enqueue( (struct List *) &MVLVList, &MVLVNodes[ i ] );
  489.  
  490.    // Make the list:
  491.    (void) MakeMemoryList();   
  492.  
  493.    ModifyListView( MVGadgets[ MemLV ], MVWnd, 
  494.                    (struct List *) &MVLVList, NULL
  495.                  );
  496.  
  497.    GT_RefreshWindow( MVWnd, NULL );
  498.    return( 0 );
  499. }
  500.  
  501. PUBLIC int ViewMemory( int type, ULONG startaddress, ULONG endaddress )
  502. {
  503.    CurrentAddress = startaddress;
  504.  
  505.    if (type == MEMF_CHIP)
  506.       sprintf( description, 
  507.                "You've selected Chip Memory (%08LX-%08LX):", 
  508.                startaddress, endaddress
  509.              );
  510.    else if (type == MEMF_FAST)
  511.       sprintf( description, 
  512.                "You've selected Expansion Memory (%08LX-%08LX):", 
  513.                startaddress, endaddress
  514.              );
  515.       
  516.    if (OpenMVWindow( description ) < 0)
  517.       {
  518.       int ans = 0;
  519.       
  520.       ans = Handle_Problem( "Couldn't open Memory Viewer!", 
  521.                             "System Problem:", NULL
  522.                           );
  523.       return( ans );
  524.       }
  525.        
  526.    (void) InitMemoryNodes( CurrentAddress );
  527.  
  528.    (void) HandleMVIDCMP();
  529.    MVCloseWindow();        // Just in case.
  530.    
  531.    return( 0 );
  532. }
  533.  
  534. /* ------------------- END of SysMemViewer.c file! --------------- */
  535.